require "Net/send_Msg"
require "proto/tower/message/ReqTowerBossMessage"
require "proto/tower/message/ReqTowerGetRanksMessage"
require "proto/tower/message/ReqTowerBattleRecordsMessage"
require "proto/tower/message/ReqTowerInfoMessage"
require "proto/tower/message/ReqTowerJumpMessage"

towerHandler = towerHandler or {};

function towerHandler.receiveTowerInfo(msg)
    --log("towerHandler.receiveTowerInfo");
    --dataTable.PrintTable(msg);
    if msg.state == 0 then
        logicMgr.TowerManager.saveTowerInfo(msg);
    end
    FireEvent(Config.EventType.Fresh_Tower);
end

--- @param msg ResTowerBossResultMessage
function towerHandler.receiveTowerBossResult(msg)
    if msg.state ~= 0 then
        logError("进入战斗错误-战斗返回失败 code:"..msg.state)
        FireEvent(Config.EventType.LoadingCancelByFightError,"战斗返回失败")
        return
    end
    --胜利层数+1
    if msg.fightResult == 1 then
        --logicMgr.FlashGiftManager.checkIsNeedAddNewFlashGift(EnumConst.FlashGiftType.TOWER, msg.layerId);

        logicMgr.TowerManager.addLayer(msg.towerType, msg.layerId + 1, msg.todayLayer);

        -- iOS Widget: 更新热砂遗迹进度
        if CompareOriginVersion("1.4.2.0001") then
            if msg.towerType == 1 then
                LoginSDK.GetInstance():SetWidgetUserTower(msg.layerId)
            end
        end
    end
    logicMgr.TowerManager.setIsCanSkip(msg.jump == 1);
    FightManager.InitRewards(msg.items)
    BattlePlaybackManager.RequestReport(msg.fightId,msg.fightResult,msg.record)

    msg.items = nil



    --[[BattlePlaybackManager.setBattleRecord(msg.record)
    BattlePlaybackManager.setBattleResult(msg.fightResult)
    BattlePlaybackManager.requestFightCmdThenPlay(msg.record, EnumConst.FightTypeConst.TOWER, function()
        closeUI(PanelNames.UIClimbTower);
        closeUI(PanelNames.UIAdventureMain);

        --胜利层数+1
        if BattlePlaybackManager.getBattleResult() == 1 then
            logicMgr.FlashGiftManager.checkIsNeedAddNewFlashGift(EnumConst.FlashGiftType.TOWER, msg.layerId);

            logicMgr.TowerManager.addLayer(msg.towerType, msg.layerId + 1);
        end

        --playCommonByType(Config.SoundType.ST_UI_battle_boss);
    end, function()
        UIBottom.onClickAdventure();
        showUI(PanelNames.UIClimbTower);
    end);]]
end

---爬塔跳关成功
function towerHandler.receiveTowerJumpHandler(msg)
    if msg.state == 0 then
        logicMgr.TowerManager.addLayer(msg.towerType, msg.layerId + 1, msg.todayLayer);
        if FightManager.AutoGoFight(EnumConst.FightTypeConst.TOWER) then
            logicMgr.TowerManager.towerAutoBattle();
        else
            FightManager.InitRewards(msg.items);
            showUI(PanelNames.UICommonItemGet, nil, nil, 3, FightManager.resultItems, logicMgr.FlashGiftManager.showNewFlashGift);
        end
        FightManager.autoJump = nil;

        -- iOS Widget: 更新热砂遗迹进度
        if CompareOriginVersion("1.4.2.0001") then
            if msg.towerType == 1 then
                LoginSDK.GetInstance():SetWidgetUserTower(msg.layerId)
            end
        end
    end
end
--请求爬塔跳关
function towerHandler.sendTowerJump(towerType)
    local req = ReqTowerJumpMessage.New()
    req.towerType = towerType;
    sendReqMessage(req)
end
--- @param msg ResTowerGetRanksMessage
function towerHandler.receiveTowerGetRanks(msg)
    if msg.state ~= 0 then
        return
    end

    logicMgr.TowerManager.setRanks(msg)
end

--- @param msg ResTowerBattleRecordsMessage
function towerHandler.receiveTowerRecords(msg)
    if msg.state ~= 0 then
        return
    end

    local playerDataList = {};
    local towerRecord = msg.records;
    for i = 1, #towerRecord do
        local playerData = {};
        playerData["rawRecord"] = towerRecord[i];
        playerData["playerId"] = tonumber(tostring(towerRecord[i].attacker.playerId));
        playerData["playerName"] = towerRecord[i].attacker.playerName;
        playerData["icon"] = towerRecord[i].attacker.pic;
        playerData["headFrame"] = towerRecord[i].attacker.picFrame;
        --playerData["level"] = towerRecord[i].attacker.level;
        playerData["fightId"] = tonumber(tostring(towerRecord[i].fightId));
        --playerData["record"] = towerRecord[i].record;
        playerData["time"] = tonumber(tostring(towerRecord[i].createTime));
        playerData["power"] = tonumber(tostring(towerRecord[i].attacker.power));
        playerData["heroList"] = {};
        local fomation = towerRecord[i].attacker.members;
        for i = 1, #fomation do
            local partnerModelId = tostring(fomation[i].partnerModelId);
            local partnerInfo = {};
            partnerInfo["partnerId"] = fomation[i].partnerModelId;
            partnerInfo["partnerModelId"] = partnerModelId
            partnerInfo["level"] = fomation[i].level;
            partnerInfo["star"] = fomation[i].star;
            partnerInfo["icon"] = logicMgr.HeroManager.GetRoleIcon();
            partnerInfo["quality"] = logicMgr.HeroManager.starToQuality(fomation[i].star)
            table.insert(playerData["heroList"],partnerInfo);
        end
        table.insert(playerDataList,playerData)
    end
    logicMgr.TowerManager.setClimbTowerRecordData({
        playerDataList = playerDataList,
        curTowerLevel = msg.layerId + 1
    });
    FireEvent(Config.EventType.Fresh_TowerRecord)
end

function towerHandler.sendTowerBoss(towerType,layerId,formation)
    local req = ReqTowerBossMessage.New();
    req.towerType = towerType;
    req.layerId = layerId;
    req.formation = formation;
    local curTowerType = logicMgr.TowerManager.getClimbType();
    local formationType = G.dataTable["t_towertype"][tostring(curTowerType)]["f_FormationType"];
    logicMgr.FormationManager.SetFormation(formationType,formation);
    req.version = Config.ProtoVersion;
    sendReqMessage(req,0,callback);
end

--发送通关记录请求并返回数据
function towerHandler.sendPassRecord(towerLevel, towerType)
    local req = ReqTowerBattleRecordsMessage.New()
    req.towerType = towerType
    req.layerId = towerLevel
    sendReqMessage(req);
end

--发送排行榜请求并返回数据
function towerHandler.sendGetRanks(towerType, rankVersion)
    local req = ReqTowerGetRanksMessage.New()
    req.towerType = towerType
    req.rankVersion = rankVersion
    sendReqMessage(req)
end

--发送阵容请求并返回数据
function towerHandler.sendGetTowerInfo()
    local req = ReqTowerInfoMessage.New()
    sendReqMessage(req)
end